<?php
  require_once('../../export/Worksheet.php');
  require_once('../../export/Workbook.php');
  // koneksi ke mysql
  require_once('../../config/koneksi.php');

  // function untuk membuat header file excel
  function HeaderingExcel() {
	  $tgl = date("dmY");
      header("Content-type: application/vnd.ms-excel");
      header("Content-Disposition: attachment; filename=master_obat_$tgl.xls" );
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
      header("Pragma: public");
      }

  // membuat header file excel dan nama filenya
  HeaderingExcel('Data Master Obat.xls');

  // membuat workbook baru
  $workbook = new Workbook("");
  // membuat worksheet ke-1 (data laki-laki)
  $worksheet1 =& $workbook->add_worksheet('Data Obat');

  // setting format header tabel data
  $format =& $workbook->add_format();
  $format->set_align('vcenter');
  $format->set_align('center');
  $format->set_color('white');
  $format->set_bold();
  $format->set_italic();
  $format->set_pattern();
  $format->set_fg_color('green');

  // membuat header tabel dengan format
  $worksheet1->set_row(0, 15);
  $worksheet1->set_column(0, 0, 5);
  $worksheet1->write_string(0, 0, "NO", $format);
  $worksheet1->set_column(0, 1, 10);
  $worksheet1->write_string(0, 1, "KODE", $format);
  $worksheet1->set_column(0, 2, 50);
  $worksheet1->write_string(0, 2, "NAMA OBAT", $format);
  $worksheet1->set_column(0, 3, 15);
  $worksheet1->write_string(0, 3, "KODE SATUAN", $format);
  $worksheet1->set_column(0, 4, 10);
  $worksheet1->write_string(0, 4, "SATUAN", $format);

  // menampilkan data mhasiswa laki-laki

  $query = "SELECT * FROM master_obat m inner join satuan s on m.id_satuan = s.id_satuan";
  $hasil = mysql_query($query);
  $baris = 1;
  while ($data = mysql_fetch_array($hasil))
  {
		$no++;
        $worksheet1->write_string($baris, 0, $no);
        $worksheet1->write_string($baris, 1, $data['kode_obat']);
        $worksheet1->write_string($baris, 2, $data['nama_obat']);
		$worksheet1->write_number($baris, 3, $data['id_satuan']);
		$worksheet1->write_string($baris, 4, $data['satuan']);
        $baris++;
  }

  // membuat worksheet ke-2 untuk data mhs perempuan
  $worksheet2 =& $workbook->add_worksheet('Data Satuan');

  // membuat header tabel
  $worksheet2->set_row(0, 15);
  $worksheet2->set_column(0, 0, 5);
  $worksheet2->write_string(0, 0, "KODE", $format);
  $worksheet2->set_column(0, 1, 30);
  $worksheet2->write_string(0, 1, "SATUAN", $format);

  // menampilkan data mhasiswa perempuan

  $query = "SELECT * FROM satuan";
  $hasil = mysql_query($query);
  $baris = 1;
  while ($data = mysql_fetch_array($hasil))
  {
        $worksheet2->write_string($baris, 0, $data['id_satuan']);
        $worksheet2->write_string($baris, 1, $data['satuan']);
        $baris++;
  }

  $workbook->close();
?>